package MyReentrant;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Fun {
    public long count = 0;
    public Lock lock = new ReentrantLock();
    public void add(){
        for (int i = 0; i < 500; i++) {
            if (lock.tryLock()) {
                try {
                    System.out.println(++count);
                }finally {
                    lock.unlock();
                }
            } else {
                System.out.println(Thread.currentThread().getName() + "==> 没有获取到锁，不等待锁释放直接跳过");
            }
        }
    }
}
